var Current = 0;
var Index = {
    liveFall: '',
    loading: false,
    searchEvents: function () {
        $('#btn-search').on('click', gotoSearh);

        function gotoSearh() {
            $('.search_input').submit();
        }
    },
    topNavScroller: function () {
        // var _mtop = $('.breadtrip-logo').offset().top;
        var _mtop = 15;
        var _scroll = true;
        var _stop;

        $(window).on('scroll',function(){
            if (_scroll === true) {
                _scroll = false;
                _stop = Math.round($(window).scrollTop());
                
                // nav scroll interaction
                if (_stop >= _mtop) {
                    $('#header').removeClass('new-index');
                    $('.top-nav').addClass('fixed');
                    $('#float-alarm').show();
                } else {
                    $('#header').addClass('new-index');
                    $('.top-nav').removeClass('fixed');
                    $('#float-alarm').hide();
                }

                setTimeout(function(){
                    _scroll = true;
                },20);
            }
        });
    },
    sliderEvents: function () {
        $("#features .items .item").each(function() {
            $(this).css("width", $(window).width());
        });

        var clickdirection = 1;
        $('.navi-next').click(function(){
            clickdirection = 1;
        });
        $('.navi-prev').click(function(){
            clickdirection = 0;
        });
        $(".scrollable").scrollable({circular: true,
                                      mousewheel: false,
                                      next: ".navi-next",
                                      prev: ".navi-prev"})
            .autoscroll({interval: 7000});
        var api = $(".scrollable").data("scrollable");
        //首页新增推广大图使用
        // api.onBeforeSeek(function(e) {
        //     if ( ( clickdirection == 1 && this.getIndex() === -1 ) || ( clickdirection == 0 && this.getIndex() === 1 )) {
        //         $('#header').addClass('new-index-521');
        //     }else{
        //         $('#header').removeClass('new-index-521');
        //     }
        // });

        $(".index_v4 #features").on("mouseover", function() {
            $(this).find(".navi-prev").show();
            $(this).find(".navi-next").show();
        });

        $(".index_v4 #features").on("mouseleave", function() {
            $(this).find(".navi-prev").hide();
            $(this).find(".navi-next").hide();
        });

        var api = $(".scrollable").data("scrollable");
        api.onSeek(function() {
            Current = this.getIndex();
            clickdirection = 1;
        });
        if (!$.browser.msie || ($.browser.msie && $.browser.version > 8)) {
            var div = $("<div></div>");
            div.addClass("extra-info");
            $("#bg0").append(div);
        } else if ($.browser.msie && $.browser.version <= 8 && $.browser.version != 6) {
            $("#bg0 .bg").addClass("bg0-IE");
        }
        $('.index-icons-create').on('click', function(e) {
            e.preventDefault();
            if ( !BT_USER_ID ) {
                bt.misc.redirect_to_login();
            } else{
                bt.misc.showPopup($('#new-trip-popup'));
            }
        });
    },
    liveEvents: function () {
        Index.liveFall = new WarterFall({
            fallCount: 4,
            fallClassPrefix: "col-"
        });
        Index.addToWaterFall();
        Index.initLoadMoreLiveEvent();
        $(".pic img").each(function() {
            $(this).fadeIn();
        });

        $(".pic img").load(function() {
            $(this).fadeIn();
        });
    },
    loadMoreLive: function(count) {
        if(Index.loading) {
            return;
        }
        $('#load-more').find('img').show();
        Index.loading = true;
        count = count || 12;
        var before = $(".talent-flow").attr("data-before");
        $.ajax({
            url: "/index_live_more/",
            data: "count=" + count + "&before=" + before,
            success: function(resp) {
                $(".tmp-container").html(resp);
                Index.addToWaterFall();
                $(".talent-flow").attr("data-before", $(".before").data("before"));
                $(".before").remove();
                $('#load-more').find('img').hide();
                Index.loading = false;
            }
        });
    },
    initLiveItemEvents: function(obj) {
        obj = $(obj);
        obj.find(".pic img").load(function() {
            $(this).fadeIn();
        });

        obj.find(".need-login").click(function() {
            bt.misc.redirect_to_login();
        });
    },
    addToWaterFall: function() {
        var items = $(".talent .tmp-container .feed");
        for(var i = 0; i < items.length; i ++) {
            Index.liveFall.addItem(items[i]);
            Index.initLiveItemEvents(items[i]);
        }
    },
    initLoadMoreLiveEvent: function() {
        $('#load-more').on('click', function(e) {
            e.preventDefault();
            Index.loadMoreLive();
        });
    }
};

var WarterFall = function(opts) {
    var defaultOptions = {
        "fallCount": 1,
        "fallClassPrefix": "col-"
    };
    this.opts = defaultOptions;
    for(i in opts) {
        this.opts[i] = opts[i];
    }
};

WarterFall.prototype.getShortestFall = function() {
    var shortestFall = $('.' + this.opts.fallClassPrefix + '0');
    var minHeight = shortestFall.height();
    for(var i = 1; i < this.opts.fallCount; i ++) {
        var fall = $('.' + this.opts.fallClassPrefix + i);
        if(minHeight > fall.height()) {
            minHeight = fall.height();
            shortestFall = fall;
        }
    }
    return shortestFall;
};

WarterFall.prototype.addItem = function (item) {
    var fall = this.getShortestFall();
    fall.append($(item));
};

$(document).ready(function() {
    Index.topNavScroller();
    Index.searchEvents();
    Index.sliderEvents();
    Index.liveEvents();
});

$(window).resize(function() {
    var window_w = $(window).width();
    var min_w = $("#content .center-align").width();
    if (window_w < min_w) {
        window_w = min_w;
    }
    $("#features .items .item").each(function() {
        $(this).css("width", window_w);
    });
    $("#features .items").css("left", 0 - window_w * (Current + 1));

});
